package it.valeandre.omino.adapter;

import it.valeandre.omino.R;
import it.valeandre.omino.model.EventDetails;
import it.valeandre.omino.model.Match;
import it.valeandre.omino.model.ScoreListItem;
import it.valeandre.omino.model.ScoreListItems;
import it.valeandre.omino.utils.Utils;

import java.text.SimpleDateFormat;
import java.util.Locale;

import android.content.Context;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ScoresAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	// private Results mResults = new Results();
	private ScoreListItems mItems = new ScoreListItems();
	private LinearLayout mScorersView;
    private Locale mLocale = Locale.ITALY;
    private Context mContext;
	
	private SimpleDateFormat mDateFormat = new SimpleDateFormat("EEE, dd/MM/yyyy HH:mm", Locale.ITALIAN) ;

	public ScoresAdapter(Context context) {
        mContext = context;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mScorersView = (LinearLayout) mInflater.inflate(R.layout.goal_detail_item, null);
	}

	public void setData(ScoreListItems results) {
		mItems = results;
		notifyDataSetChanged();
	}

	public int getCount() {
		return mItems.getScoreListItems().size();
	}

	public ScoreListItem getItem(int pos) {
		return mItems.getScoreListItems().get(pos);
	}

	public long getItemId(int pos) {
		return pos;
	}

	public View getView(int position, View convertView, ViewGroup adapterView) {
//		View view = convertView;
//		if (view == null) {
		View	view = mInflater.inflate(R.layout.scores_item_new, null);
//		}

		Utils.log("parent = " + view.getLayoutParams());
		LinearLayout headerContainer = (LinearLayout) view.findViewById(R.id.scores_item_header_container);
		LinearLayout scoreContainer = (LinearLayout) view.findViewById(R.id.scores_item_score_container);
		// View divider = (View) view.findViewById(R.id.scores_item_divider);
		ScoreListItem item = getItem(position);
		if (item.isHeader()) {
			headerContainer.setVisibility(View.VISIBLE);
			scoreContainer.setVisibility(View.GONE);
			// divider.setVisibility(View.GONE);

			TextView date = (TextView) view.findViewById(R.id.scores_item_header_date);
			date.setText(item.getGmtDate());

		} else {
			Match match = (Match) item;

			headerContainer.setVisibility(View.GONE);
			scoreContainer.setVisibility(View.VISIBLE);
			// if(item.isLast()) {
			// divider.setVisibility(View.GONE);
			// } else {
			// divider.setVisibility(View.GONE);
			// }

			TextView homeTeam = (TextView) view.findViewById(R.id.results_item_home_team_name);
			TextView visitorTeam = (TextView) view.findViewById(R.id.results_item_away_team_name);
			TextView homeTeamResult = (TextView) view.findViewById(R.id.results_item_home_team_result);
			TextView awayTeamResult = (TextView) view.findViewById(R.id.results_item_away_team_result);
			TextView stadium = (TextView) view.findViewById(R.id.results_item_stadium);
			TextView date = (TextView) view.findViewById(R.id.results_item_date);
//			LinearLayout homeDetails = (LinearLayout) view.findViewById(R.id.score_toolbar_home_goals_container);
//			LinearLayout awayDetails = (LinearLayout) view.findViewById(R.id.score_toolbar_away_goals_container);

//			fillDetails(homeDetails, match);
//			fillDetails(awayDetails, match);
			
			String hometeamString = match.getHometeam() == null || match.getHometeam().trim().equals("") ? match.getHometeamRealtime() : match.getHometeam();
			String visitorteamString = match.getAwayteam() == null || match.getAwayteam().trim().equals("") ? match.getAwayteamRealtime() : match.getAwayteam();
			
			homeTeam.setText(hometeamString);
			visitorTeam.setText(visitorteamString);
			homeTeamResult.setText(match.getHomeGoals());
			awayTeamResult.setText(match.getAwayGoals());
			if (match.getStadium() == null) {
				stadium.setVisibility(View.GONE);
			} else {
				stadium.setText(match.getStadium());
			}
			
//			Date matchDate = Utils.convertGmtStringToDate(match.getDate(), mDateFormat);
//			String dateString Utils.convertLocale(match.getDate(), in, out, inDateFormat, outDateFormat)
            String formattedDate = Utils.convertGmtStringToString(match.getDate(), "yyyy-MM-dd'T'hh:mm:ssZ", "HH:mm", mLocale);
			date.setText(mContext.getResources().getString(R.string.hours) + " " + formattedDate);

		}

		return view;
	}

	private void fillDetails(LinearLayout container, Match match) {
		EventDetails goalDetails = match.getHomeGoalDetailsPairList();
		for(Pair<String, String> pair: goalDetails.getEventDetailPairList()) {
			TextView minute = (TextView) mScorersView.findViewById(R.id.goal_detail_item_minute);
			TextView scorer = (TextView) mScorersView.findViewById(R.id.goal_detail_item_scorer);
			minute.setText(pair.first);
			scorer.setText(pair.second);
			if(mScorersView.getLayoutParams() == null) {
				LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
				mScorersView.setLayoutParams(params);
			}
//			((ViewGroup)mScorersView.getParent()).removeAllViews();
			container.addView(mScorersView);
			
		}
		
		
		
	}

	// OLD FEED
	// public View getView(int position, View convertView, ViewGroup
	// adapterView) {
	// View view = convertView;
	// if (view == null) {
	// view = mInflater.inflate(R.layout.scores_item, null);
	// }
	// LinearLayout headerContainer = (LinearLayout)
	// view.findViewById(R.id.scores_item_header_container);
	// LinearLayout scoreContainer = (LinearLayout)
	// view.findViewById(R.id.scores_item_score_container);
	// View divider = (View) view.findViewById(R.id.scores_item_divider);
	// ScoreListItem item = getItem(position);
	// if (item.isHeader()) {
	//
	// headerContainer.setVisibility(View.VISIBLE);
	// scoreContainer.setVisibility(View.GONE);
	// divider.setVisibility(View.GONE);
	//
	// TextView date = (TextView)
	// view.findViewById(R.id.scores_item_header_date);
	// date.setText(item.getGmtDate());
	//
	// } else {
	// Result result = (Result) item;
	//
	// headerContainer.setVisibility(View.GONE);
	// scoreContainer.setVisibility(View.VISIBLE);
	// if(item.isLast()) {
	// divider.setVisibility(View.GONE);
	// } else {
	// divider.setVisibility(View.VISIBLE);
	// }
	//
	// ImageView logo = (ImageView)
	// view.findViewById(R.id.results_last_goal_team);
	// TextView homeTeam = (TextView)
	// view.findViewById(R.id.results_item_home_team_name);
	// TextView visitorTeam = (TextView)
	// view.findViewById(R.id.results_item_away_team_name);
	// TextView homeTeamResult = (TextView)
	// view.findViewById(R.id.results_item_home_team_result);
	// TextView awayTeamResult = (TextView)
	// view.findViewById(R.id.results_item_away_team_result);
	//
	// logo.setVisibility(View.GONE);
	// homeTeam.setText(result.getHomeTeam());
	// visitorTeam.setText(result.getAwayTeam());
	// homeTeamResult.setText(result.getHomeScore());
	// awayTeamResult.setText(result.getAwayScore());
	// }
	//
	// return view;
	// }

	public int getItemViewType(int pos) {
		if (getItem(pos).isHeader()) {
			return 0;
		} else {
			return 1;
		}

	}

	public int getViewTypeCount() {
		return 2;
	}

	public boolean hasStableIds() {
		return true;
	}

	public boolean isEmpty() {
		return mItems.getScoreListItems().isEmpty();
	}

	public boolean areAllItemsEnabled() {
		return false;
	}

	public boolean isEnabled(int position) {
		return !getItem(position).isHeader();
	}

}
